//
//  sendViewController.swift
//  NewLndd
//
//  Created by 邵禹曦 on 2020/1/7.
//  Copyright © 2020 辽宁当代. All rights reserved.
//

import UIKit
// MARK: - 订购
class sendViewController: HomeApiViewController {
    // 商品代码
    @IBOutlet weak var coi: UILabel!
    // 商品名称
    @IBOutlet weak var con: UILabel!
    // 订购价
    @IBOutlet weak var price: UILabel!
    // 订购数量
    @IBOutlet weak var num: UILabel!
    // 最小订购数量
    @IBOutlet weak var minNum: UILabel!
    // 最大订购数量
    @IBOutlet weak var maxNum: UILabel!
    // 最小变动数量
    @IBOutlet weak var minChangeNum: UILabel!
    // 订购开始时间
    @IBOutlet weak var starTime: UILabel!
    // 订购结束时间
    @IBOutlet weak var endTime: UILabel!
    // 最大可定数量
    @IBOutlet weak var maxYnum: UILabel!
    
    // 订购数量
    @IBOutlet weak var dNumber: UITextField!
    // 提交
    @IBOutlet weak var tj: UIButton!
    // 返回
    @IBOutlet weak var ret: UIButton!
    
    // 调用Model
    var orderRet : comOrder?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        showInfo()
    }
    

}


extension sendViewController {
    private func showInfo() {
           // GCD
           let group = DispatchGroup()
    
           // 从沙盒中获取参数
            var userPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
            userPath =  (userPath as NSString).appendingPathComponent("data.plist")
            user = NSKeyedUnarchiver.unarchiveObject(withFile: userPath) as? User
        Thread.sleep(forTimeInterval: 0.05)


           // 用户验证
           self.u = user?.U!
           self.retcode = user?.RETCODE!
           // 用户验证
           let str = [u,"101",retcode]
            
           //self.isLogin = self._network(str: str as! [String], group: group)
        group.enter()
        NetworkTools.share.check_user(code: str as Array<Any>) { (result, error) in
                      
                          if error != nil {
                              print("no no no")
                              return
                          }
                          

                          if Int(self._retcode(res: result!))! < 0 {
                              self.isLogin = false
                          } else {
                              self.isLogin = true
                          }
                          group.leave()
                      }

           group.notify(queue: DispatchQueue.main) {
               if !self.isLogin {
                   return self.setupVisitorView()
               } else {
                Thread.sleep(forTimeInterval: 0.05)
                   // 2.设置导航栏内容
                   //self.setupNavigationBar()
                   self.tableShouInfo()
               }
            }
       }
}

extension sendViewController {
    @objc private func tableShouInfo() {
        self.coi.text! = (self.orderRet?.coi)!
        self.con.text! = (self.orderRet?.con)!
        self.price.text! = (self.orderRet?.price)!
        self.num.text! = (self.orderRet?.num)!
        self.minNum.text! = (self.orderRet?.minNum)!
        self.maxNum.text! = (self.orderRet?.maxNum)!
        self.minChangeNum.text! = (self.orderRet?.minChangeNum)!
        self.starTime.text! = (self.orderRet?.starTime)!
        self.endTime.text! = (self.orderRet?.endTime)!
        
        
        
        setupView()
    }
    
    @objc func setupView() {
        // 监听登陆按钮
        tj.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)

        // 返回上一页按钮
        ret.addTarget(self, action: #selector(backBtnClick), for: .touchUpInside)
    }
    
    
    // 绑定登陆按钮
    @objc private func loginBtnClick()
    {
        guard self.dNumber.text != "" else {
            if #available(iOS 13.0, *) {
                self.showMsgbox(_message: "订购数量不能为空")
            }
            return
        }
        
        
        let vcoi : String = self.coi.text!
        let dnum : String = self.dNumber.text!
        
        let str = [user?.U!, user?.RETCODE!, vcoi, dnum]
        print(str)
        
        NetworkTools.share.issue_apply(code: str as Array<Any>) { (result, error) in
            
            if error != nil {
                print("no no no")
                return
            }
            

            
            // 获取可选类型中的数组
            guard let resultArray = result else {
                print("no")
                return
            }
            print(resultArray)
            if let REP = resultArray["REP"] as? [String : AnyObject] {
                if let arr = REP["RESULT"] as? [String : AnyObject]   {
                    let code : Int = Int(arr["RETCODE"] as! String)!
                    if(Int(truncating: NSNumber(value: code)) >= 0) {
                        if #available(iOS 13.0, *) {
                            self.showMsgbox(_message: "请求成功")
                        }
                    } else {
                        if #available(iOS 13.0, *) {
                            self.showMsgbox(_message: "请求失败")
                        }
                    }
                }
            }
            
            
    
        }
    }
    
    // 返回上一页
    @objc private func backBtnClick()
    {
        self.dismiss(animated: true, completion: nil)
    }
}
